package com.example.demo.configuration;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import javax.servlet.MultipartConfigElement;
import java.nio.charset.Charset;

@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private TokenInterceptor tokenInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler( "/img/**" ).addResourceLocations( "file:/blog/source/" );
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }
//    @Bean
//    public HttpMessageConverter<String> responseBodyConverter() {
//        StringHttpMessageConverter converter = new StringHttpMessageConverter( Charset.forName("UTF-8"));
//        return converter;
//    }

    public WebConfig() {
    }

    /**
     * addInterceptor：需要一个实现HandlerInterceptor接口的拦截器实例
     * addPathPatterns：用于设置拦截器的过滤路径规则；addPathPatterns("/**")对所有请求都拦截
     * excludePathPatterns：用于设置不需要拦截的过滤规则
     * 拦截器主要用途：进行用户登录状态的拦截，日志的拦截等。
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor( this.tokenInterceptor )
                .addPathPatterns( new String[]{"/**"} )
                .excludePathPatterns( new String[]{"/user/*", "/homePage/findHome",
                        "/swagger-ui.html", "/webjars/**","/swagger-resources",
                        "/homePage/findHome/json", "/homePage/findHome/text",
                        "/img/**", "/blog/**", "/demo/**", "/user/captcha", "/user/noToken/*", "/user/codeBySMS",
                        "/user/loginBySMS", "/user/systemInfo", "/favicon.ico", "/error", "/login",
                        "/user/codeByEmail", "/noToken/**", "/publicController/initSystemTable", "/user/forgetPassword", "/user/verificationCode", "/publicController/selectTree/A_USER_SYSTEMINFO", "/user/setNewPassword", "/user/sendCode", "/publicController/selectTree/A_USER_ROLE_GROUP", "/publicController/getColumns", "/develop/**", "/source/**", "/file/image/**/**", "/userProject/login", "/emailManage/**", "/webApp/**"} );
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping( "/**" ).allowedOrigins( new String[]{"*"} ).allowCredentials( true ).allowedHeaders( new String[]{"*"} ).allowedMethods( new String[]{"GET", "POST", "DELETE", "PUT", "PATCH"} ).maxAge( 3600L );
    }
}
